package remote;

import java.io.*;
import java.util.ArrayList;

public class IOfile<E extends Serializable> {
	String filename;
	File file;
	
	public IOfile(String filename){
		this.filename=filename;
		file=new File(filename);
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<E> readFile(){
		ArrayList<E> list=new ArrayList<>();		
		
		try{
			 FileInputStream fileStream = new FileInputStream(file);
			 ObjectInputStream os = new ObjectInputStream(fileStream);
			
			 if(fileStream!=null){				 
				 while(fileStream!=null){
					  	list.add((E)os.readObject());
					    }
				 os.close();					   
			 }			
		}catch(Exception ex){}
		
		return list;
	}
	
	public boolean writeFile(ArrayList<E> list){
		try {
			FileOutputStream fileStream = new FileOutputStream(file);
			ObjectOutputStream os = new ObjectOutputStream(fileStream);
			for (E e : list) {
				os.writeObject(e);
			}
			os.close();
		} catch (Exception e) {	
			return false;
		}
		return true;
	}
}
